import { createRouter, createWebHistory } from 'vue-router'
import { useGlobalStore } from '../store/userInfo'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('../components/Login.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('../components/Register.vue'),
      meta: {
        title: '注册'
      }
    },
    // 用户端路由
    {
      path: '/front',
      name: 'frontHome',
      component: () => import('../views/FrontHome.vue'),
      meta: {
        title: '旅游爱好者首页'
      },
      children: [
        {
          path: '',
          name: 'FrontIndex',
          component: () => import('../views/FrontIndex.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'travel-announcement',
          name: 'TravelAnnouncement',
          component: () => import('../views/TravelAnnouncement.vue'),
          meta: {
            title: '旅行公告',
            requireAuth: true
          }
        },
        {
          path: 'travel-announcement/:id',
          name: 'TravelAnnouncementDetail',
          component: () => import('../views/TravelAnnouncementDetail.vue'),
          meta: {
            title: '公告详情',
            requireAuth: true
          }
        },
        {
          path: 'travel-application',
          name: 'TravelApplication',
          component: () => import('../views/TravelApplication.vue'),
          meta: {
            title: '旅行申请',
            requireAuth: true
          }
        },
        {
          path: 'member-application',
          name: 'MemberApplication',
          component: () => import('../views/MemberApplication.vue'),
          meta: {
            title: '入团申请',
            requireAuth: true
          }
        },
        {
          path: 'forum',
          name: 'Forum',
          component: () => import('../views/Forum.vue'),
          meta: {
            title: '交流论坛',
            requireAuth: true
          }
        },
        {
          path: 'forum/:id',
          name: 'ForumDetail',
          component: () => import('../views/ForumDetail.vue'),
          meta: {
            title: '话题详情',
            requireAuth: true
          }
        },
        {
          path: 'user-center',
          name: 'UserCenter',
          component: () => import('../views/UserCenter.vue'),
          meta: {
            title: '个人中心',
            requireAuth: true
          }
        }
      ]
    }
  ]
})


router.beforeEach((to, from, next) => {
    const store = useGlobalStore()
  const token = store.token
  const userInfo = store.userInfo
  // 设置页面标题
  if (to.meta.title) {
    document.title = to.meta.title
  }
  
  // 检查是否需要登录
  if (to.meta.requireAuth) {
    if (token) {
      // 检查是否需要管理员权限
      if (to.meta.requireAdmin) {
        // 在实际应用中，这里应该检查用户是否有管理员权限
        // 这里简单模拟，假设用户名为admin的是管理员
        const isAdmin = userInfo.name === 'admin'
        if (isAdmin) {
          next()
        } else {
          next('/front') // 非管理员重定向到用户端首页
        }
      } else {
        next()
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})

export default router